package handler

import (
	"context"
	"strconv"

	"gitee.com/jn-shao/common"

	"routeSrv/domain/model"
	"routeSrv/domain/service"
	Route "routeSrv/proto/routeSrv"
)

type RouteHandler struct {
	//注意这里的类型是 IRouteDataService 接口类型
	RouteDataService service.IRouteDataService
}

// 添加创建 Route
func (e *RouteHandler) AddRoute(ctx context.Context, info *Route.RouteInfo, rsp *Route.Response) error {
	common.Info("添加Ingress")
	RouteModel := &model.Route{}
	if err := common.SwapTo(info, RouteModel); err != nil {
		common.Error(err)
		rsp.Msg = err.Error()
		return err
	}
	if err := e.RouteDataService.CreateToK8s(info); err != nil {
		common.Error(err)
		rsp.Msg = err.Error()
		return err
	} else {
		//操作数据库写入数据
		RouteID, err := e.RouteDataService.AddRoute(RouteModel)
		if err != nil {
			common.Error(err)
			rsp.Msg = err.Error()
			return err
		}
		common.Info("Ingress 添加成功数据库ID号为：" + strconv.FormatInt(RouteID, 10))
		rsp.Msg = "Ingress 添加成功数据库ID号为：" + strconv.FormatInt(RouteID, 10)
	}

	return nil
}

// 删除 Route
func (e *RouteHandler) DeleteRoute(ctx context.Context, req *Route.RouteId, rsp *Route.Response) error {
	//先查找数据
	routeModel, err := e.RouteDataService.FindRouteByID(req.Id)
	if err != nil {
		common.Error(err)
		return err
	}
	if err := e.RouteDataService.DeleteFromK8s(routeModel); err != nil {
		common.Error(err)
		return err
	} else {
		if err := e.RouteDataService.DeleteRoute(req.Id); err != nil {
			common.Error(err)
			return err
		}
		common.Info("删除Ingress ID：" + strconv.FormatInt(req.Id, 10))
	}
	return nil
}

// 更新指定的 Route
func (e *RouteHandler) UpdateRoute(ctx context.Context, req *Route.RouteInfo, rsp *Route.Response) error {
	routeMode := &model.Route{}
	if err := common.SwapTo(req, routeMode); err != nil {
		common.Error(err)
		rsp.Msg = err.Error()
		return err
	}
	//先更新k8s中的Route信息
	if err := e.RouteDataService.UpdateToK8s(req); err != nil {
		common.Error(err)
		rsp.Msg = err.Error()
		return err
	} else {
		if err := e.RouteDataService.UpdateRoute(routeMode); err != nil {
			common.Error(err)
			rsp.Msg = err.Error()
			return err
		}
		common.Info("更新Ingress ID：" + strconv.FormatInt(req.Id, 10))
	}

	return nil
}

// 查询单个信息
func (e *RouteHandler) FindRouteByID(ctx context.Context, req *Route.RouteId, rsp *Route.RouteInfo) error {
	//查询Route数据
	routeModel, err := e.RouteDataService.FindRouteByID(req.Id)
	if err != nil {
		common.Error(err)
		return err
	}
	err = common.SwapTo(routeModel, rsp)
	if err != nil {
		common.Error(err)
		return err
	}
	return nil
}

// 查询所有 Route
func (e *RouteHandler) FindRouteList(ctx context.Context, req *Route.FindRoute, rsp *Route.RouteList) error {
	//查询所有Route
	page := &model.Page{Pages: req.Pages, PagePerNums: req.PagePerNums}
	total, routeList, err := e.RouteDataService.FindRouteList(page)
	if err != nil {
		common.Error(err)
		return err
	}

	rsp.Total = total
	//整理格式
	for _, v := range routeList {
		routeInfo := &Route.RouteInfo{}
		err := common.SwapTo(v, routeInfo)
		if err != nil {
			common.Error(err)
			return err
		}
		rsp.Data = append(rsp.Data, routeInfo)
	}
	return nil
}
